#include "json.hpp"
using json = nlohmann::json;

#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

//json序列化示例1
string func1()
{
    json js;
    js["msg_type"] = 2;
    js["from"] = "zhang san";
    js["msg"] = "hello, how are you!";

    cout<<js<<endl;

    string sendBuf = js.dump();
    cout<<sendBuf<<endl;
    return sendBuf;
}

//json序列化示例2
void func2()
{
    json js;
    //添加数组
    js["id"] = {1,2,3,4,5};
    //添加key-value
    js["name"] = "zhang san";
    //添加对象
    js["msg"]["zhangsan"] = "hello world";
    js["msg"]["liu shuo"] = "hello china";
    //上面等同于下面这句一次性添加数组对象
    js["msg"] = {{"zhang san","hello world"},{"liu shuo","hell china"}};
    cout<<js<<endl;
}

//json序列化示例代码3
void func3()
{
    json js;
    //序列化vector
    vector<int> v;
    v.push_back(2);
    v.push_back(4);
    v.push_back(6);
    js["list"] = v;

    //序列化一个map容器
    map<int,string> m;
    m.insert({1,"黄山"});
    m.insert({2,"华山"});
    m.insert({3,"泰山"});
    js["path"] = m;

    cout<<js<<endl;
}

int main()
{
    string jsonstr = func1();
    //数据反序列化
    json jsbuf = json::parse(jsonstr);
    cout<<jsbuf["msg_type"]<<endl;
    func2();
    func3();
    return 0;
}